﻿using System;
using System.Net;
using System.Windows;
using System.IO.IsolatedStorage;
using System.IO;

namespace WeiboSdkSample.Classes
{
    public class PersistHelper
    {
        public static T LoadSetttingFromStorage<T>(string Key, T defaultValue)
        {
            T ObjToLoad = default(T);
            try
            {
                if (IsolatedStorageSettings.ApplicationSettings.Contains(Key))
                {
                    ObjToLoad = (T)IsolatedStorageSettings.ApplicationSettings[Key];
                }
                else
                {
                    ObjToLoad = defaultValue;
                }
            }
            catch (Exception)
            {
                return default(T);
            }
            return ObjToLoad;
        }

        public static void SaveSettingToStorage(string Key, object Setting)
        {
            try
            {
                if (!IsolatedStorageSettings.ApplicationSettings.Contains(Key))
                {
                    IsolatedStorageSettings.ApplicationSettings.Add(Key, Setting);
                }
                else
                {
                    IsolatedStorageSettings.ApplicationSettings[Key] = Setting;
                }

                IsolatedStorageSettings.ApplicationSettings.Save();
            }
            catch (Exception)
            {
            }
        }

        public static bool IsSettingPersisted(string Key)
        {
            return IsolatedStorageSettings.ApplicationSettings.Contains(Key);
        }

        public static void DeleteSetting(string key)
        {
            try
            {
                if (IsolatedStorageSettings.ApplicationSettings.Contains(key))
                {
                    IsolatedStorageSettings.ApplicationSettings.Remove(key);
                    IsolatedStorageSettings.ApplicationSettings.Save();
                }
            }
            catch (Exception)
            {
            }
        }
    }
}
